package com.yanqu.road.server.manger;

import com.yanqu.road.entity.servercenter.ServerGroup;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.logic.bussiness.servercenter.ServerBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.plugin.ManagerReloadListener;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ServerGroupMgr extends TempMgr {

	private static final Logger logger = LogManager.getLogger(ServerGroupMgr.class.getName());

	private static Map<Integer, Map<Integer,ServerGroup>> channelServerGroupMap;

	private static Map<Integer,ServerGroup> serverGroupMap;

	private static List<ManagerReloadListener> managerReloadListener = new ArrayList<>();
	/**
	 * 分组变更前服务器列表
	 */
	private static Map<Integer, List<Long>> allBeforeChangeServerList = new ConcurrentHashMap<>();

	@Override
	public boolean stop() {
		return false;
	}

	@Override
	public boolean init() {
		return reload();
	}

	@Override
	public boolean reload() {
		reloadServerGroup();
		return true;
	}

	@Override
	public boolean reloadConfig() throws Exception {
		return true;
	}

	@Override
	public boolean reloadData() throws Exception {
		return true;
	}

	public static void reloadServerGroup(){
		Map<Integer, Map<Integer,ServerGroup>> tempChannelServerGroupMap = new ConcurrentHashMap<>();
		Map<Integer,ServerGroup> tempServerGroupMap = new ConcurrentHashMap<>();
		ServerBussiness.getServerGroupMap(tempChannelServerGroupMap, tempServerGroupMap);
		channelServerGroupMap = tempChannelServerGroupMap;
		serverGroupMap = tempServerGroupMap;
		notifyReload();
		allBeforeChangeServerList = initAllBeforeChangeServerList();
	}

	private static Map<Integer, List<Long>> initAllBeforeChangeServerList(){
		Map<Integer, List<Long>> dataMap = new ConcurrentHashMap<>();
		for(Map.Entry<Integer, Map<Integer, ServerGroup>> dataEntry : channelServerGroupMap.entrySet()){
			if(!dataMap.containsKey(dataEntry.getKey())){
				dataMap.put(dataEntry.getKey(), new ArrayList<>());
			}
			List<Long> dataList = dataMap.get(dataEntry.getKey());
			for(ServerGroup serverGroup : dataEntry.getValue().values()){
				dataList.addAll(serverGroup.getServerList());
			}
		}
		return dataMap;
	}

	public static void addReloadListener(ManagerReloadListener reloadListener){
		managerReloadListener.add(reloadListener);
	}

	private static void notifyReload(){
		if(null != managerReloadListener){
			for (ManagerReloadListener listener : managerReloadListener) {
				listener.notifyReload();
			}
		}
	}

	public static Map<Integer, ServerGroup> getServerGroupMap(int channelId){
		return channelServerGroupMap.get(channelId);
	}

	public static List<ServerGroup> getServerGroupList(int channelId){
		Map<Integer, ServerGroup> dataMap = getServerGroupMap(channelId);
		if(null != dataMap){
			return new ArrayList<>(dataMap.values());
		}
		return new ArrayList<>();
	}

	public static ServerGroup getServerGroup(int groupId){
		return serverGroupMap.get(groupId);
	}

	public static int getGroupIdByServerId(long serverId){
		int groupId = 0;
		ServerGroup serverGroup = getServerGroupByServerId(serverId);
		if(null != serverGroup){
			groupId = serverGroup.getGroupId();
		}
		return groupId;
	}

	public static ServerGroup getServerGroupByServerId(long serverId){
		ServerGroup serverGroup = null;
		ServerInfo serverInfo = ServerListMgr.getServerInfo(serverId);
		if(null == serverInfo){
			for(ServerGroup tempGroup : serverGroupMap.values()){
				if(tempGroup.getServerList().contains(serverId)){
					serverGroup = tempGroup;
					break;
				}
			}
		}else {
			Map<Integer, ServerGroup> dataMap = getServerGroupMap(serverInfo.getChannelId());
			if(null != dataMap){
				for(ServerGroup tempGroup : dataMap.values()){
					if(tempGroup.getServerList().contains(serverId)){
						serverGroup = tempGroup;
						break;
					}
				}
			}
		}
		return serverGroup;
	}

	/**
	 * 是否开放跨服榜单
	 * @param serverId
	 * @return
	 */
	public static boolean isOpenCrossRank(long serverId){
		ServerGroup serverGroup = ServerGroupMgr.getServerGroupByServerId(serverId);
		if(null == serverGroup || !serverGroup.isCrossRank()){
			return false;
		}
		return true;
	}


	/**
	 * 根据渠道编号获取变更前服务器编号
	 * @param channelId
	 * @return
	 */
	public static List<Long> getBeforeChangeServerList(int channelId){
		List<Long> dataList = new ArrayList<>();
		if(allBeforeChangeServerList.containsKey(channelId)){
			dataList.addAll(allBeforeChangeServerList.get(channelId));
		}
		return dataList;
	}
}
